/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <Windows.h>

#include <baja/common.hpp>
#include <baja/error.hpp>
#include <baja/graphics.hpp>
#include <baja/service.hpp>
#include <baja/math.hpp>
#include <baja/app/app.hpp>
#include <baja/app/app_shell.hpp>
#include <baja/app/graphics_factory.hpp>
#include <windows.ui.xaml.media.dxinterop.h>

namespace baja { namespace app { namespace directui {

namespace a = ::baja::app;
namespace g = ::baja::graphics;
namespace m = ::baja::math;

// dx11.1 + metro shell factory
// this factory class is the "glue" between a shell (e.g. metro) and a graphics engine (e.g. dx11.1)
// the purpose is to keep the app agnostic of the implementation of the shell and the graphics engine
struct graphics_factory : public igraphics_factory
{
    struct context_directui
    {
        g::shell_type shellType;
        WRL::ComPtr<ISwapChainBackgroundPanelNative> panelNative;
    };

    graphics_factory(
        const m::point2d& dimensions,
        ISwapChainBackgroundPanelNative* panelNative
        ) : _dimensions(dimensions), _panelNative(panelNative)
    {
    }

    std::shared_ptr<g::iengine> createGraphics(const std::shared_ptr<iapp_shell_base>& shell, const bool useWARP)
    {
        context_directui context;
        context.shellType = g::shell_type::directUI;
        context.panelNative = _panelNative;

        // init the display params
        g::display_params dp;
        dp.backBufferFormat = g::pixel_format::d32BitColorNoAlpha;
        dp.depthStencilFormat = g::pixel_format::d24S8;
        dp.backBufferWidth = static_cast<uint32>(_dimensions.x);
        dp.backBufferHeight = static_cast<uint32>(_dimensions.y);
        dp.backBufferCount = 1;
        dp.useFSAA = false; // TRACK: 136: multi sampling doesn't work in immersive

        // create graphics
        return g::createEngine(
            services::getMap(), // use the singleton services map 
            useWARP, 
            dp, 
            static_cast<void*>(&context)
            );
    }

private:
    m::point2d _dimensions;
    WRL::ComPtr<ISwapChainBackgroundPanelNative> _panelNative;
};

}}} // namespace baja::app::directui

namespace baja { namespace app {

inline std::shared_ptr<igraphics_factory> createGraphicsFactory(
    const m::point2d& dimensions,
    ISwapChainBackgroundPanelNative* panelNative
    )
{
    return std::make_shared<baja::app::directui::graphics_factory>(dimensions, panelNative);
}
}} // namespace baja::app